module test_actor;

import dactor.actor;
import dactor.coroutine;
import dactor.scheduler;
import std.stdio;

class Ping {
    public string message;
}

void ping_1(Pid pid) {
    writefln("ping_1 pid: %s", selfPid());
    while (true) {
        Ping m = new Ping;
        m.message = "ping_1";
        send(pid, m);
        sleep(1000);
    }
}

void ping_2() {
    writefln("ping_2 pid: %s", selfPid());
    while (true) {
        Ping m = new Ping;
        m.message = "ping_2";
        send("pong", m);
        sleep(1000);
    }
}

void pong() {
    foreach(i; 0..4) {
        Message m = receive();
        writefln("receive message %s from %s", m.message, m.from);
    }
}

void timer() {
    while (true) {
        auto m = receive(1500);
        writefln("timer receive result: %s", m);
    }
}

void main() {
    Pid pid = spawn("pong", &pong);
    writefln("pong pid: %s", pid);

    spawn(&ping_1, pid);
    spawn(&ping_2);

    spawn(&timer);

    Scheduler.instance.run();
}

